package 招聘.华为.p3;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        int N;
        Scanner s=new Scanner(System.in);
        N=s.nextInt();
        TreeMap<String,String> data=new TreeMap<>();
        for (int i = 0; i < N; i++) {
            String datum=s.next();
            String[] temp=datum.split(",");
            data.put(temp[0]+","+temp[1],temp[2]);

        }
        int M;
        M=s.nextInt();
        TreeMap<String,String> after_changed=new TreeMap<>();
        TreeMap<String,Boolean> changed=new TreeMap<>();
        for (int i = 0; i < M; i++) {
            String datum=s.next();
            String[] temp=datum.split(",");
            String passenger_name=data.get(temp[0]+","+temp[1]);

            //data.remove(temp[0]+","+temp[1]);
            after_changed.put(temp[2]+","+temp[3],passenger_name);
            changed.put(passenger_name,true);
        }
        for(Map.Entry<String, String> entry : data.entrySet()) {
            if(!changed.containsKey(entry.getValue())) {
                after_changed.put(entry.getKey(),entry.getValue());
            }
        }
        for (Map.Entry<String, String> entry : after_changed.entrySet()) {
            System.out.println(entry.getKey() +","+ entry.getValue());
        }
    }
}
